package com.my.screenadapter.searchpopwindow;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.my.screenadapter.R;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张英杰 on 2016/12/19.
 */

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {

//    http://blog.csdn.net/harvic880925/article/details/49272285
    private TextView tvSearch;
    private PopupWindow mPopWindow;
    private SearchAdapter mAdapter;
    private Button btn_search;
    private EditText et_search;
    private final String userID = "123";
    private List<FieldCache> mList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        tvSearch = (TextView) findViewById(R.id.tvSearch);
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow();
                initData();
            }
        });
    }

    private void showPopupWindow() {
        View contentView = LayoutInflater.from(SearchActivity.this).inflate(R.layout.popuplayout, null);
        mPopWindow = new PopupWindow(contentView);
        //设置可以获取焦点，否则弹出菜单中的EditText是无法获取输入的
        mPopWindow.setFocusable(true);
        mPopWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置各个控件的点击响应
        RecyclerView rv_search = (RecyclerView) contentView.findViewById(R.id.rv_searchhistory);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_search.setLayoutManager(layoutManager);
        mAdapter = new SearchAdapter();
        rv_search.setAdapter(mAdapter);
        mList = new ArrayList<>();
        et_search = (EditText) contentView.findViewById(R.id.et_input);
        btn_search = (Button) contentView.findViewById(R.id.search_button);
        btn_search.setOnClickListener(this);
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("TextListenter===","beforeTextChanged==="+s);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("TextListenter===","onTextChanged==="+s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                mAdapter.performFiltering(s);
                Log.i("TextListenter===","afterTextChanged==="+s);
            }
        });
        mAdapter.setOnClickListener(new SearchAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String content, int position, int id) {
                et_search.setText(content);
//                修改 EditText 中的光标位置
                et_search.setSelection(content.length());
//                performClick 是使用代码主动去调用控件的点击事件
                btn_search.performClick();
            }
        });
        //显示PopupWindow
        View rootview = LayoutInflater.from(SearchActivity.this).inflate(R.layout.activity_search, null);
        mPopWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);
    }

    private void initData() {
//        List<FieldCache> searchCaches = DataSupport.where("UserID = ?", userID).order("id desc").find(FieldCache.class);
//        if (searchCaches.size() > 0) {
//            for (int i = 0; i < searchCaches.size(); i++) {
//                Log.i("===", searchCaches.get(i).toString());
//            }
//            mAdapter.setData(searchCaches);
//        }
//        SparseArray sparseArray=new SparseArray();
        List<FieldCache> originData = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            FieldCache fieldCache = new FieldCache();
            fieldCache.setParcelCode("第 " + i + "条数据"+(int)(Math.random()*1000));
            originData.add(fieldCache);
        }
        mList = originData;
        mAdapter.setData(mList,et_search.getText().toString().trim());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_button:
//                saveSearchHistory();
//                initData();
        }
    }

    /*
* 保存搜索记录
*/
    private void saveSearchHistory() {
        String text = et_search.getText().toString().trim();
        if (text.length() < 1) {
            return;
        }

        // TODO: 2016/12/19 操作数据库 数据排序有误
        List<FieldCache> caches = DataSupport.where("UserID = ?", userID).find(FieldCache.class);
        if (caches.size() > 0) {
            for (int i = 0; i < caches.size(); i++) {
                FieldCache Cache = caches.get(i);
                if (Cache.getParcelCode().equals(text)) {
                    DataSupport.delete(FieldCache.class, Cache.getId());
                }
            }
        }
        FieldCache searchCache = new FieldCache();
        searchCache.setUserID(userID);
        searchCache.setParcelCode(text);
        searchCache.save();
    }
}
